import {
  getRoleList,
  getRoute,
  getRole,
  addRole,
  updateRole,
  deleteRole
} from "@/api/role"; // 引入请求

// 封装了一个默认的state数据
const getDefaultState = () => {
  return {};
};

// 赋值默认state
const state = getDefaultState();

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState()); // 用于清空重置state
  }
};

const actions = {
  // 获取权限列表
  getRoleList({ commit }, data) {
    return new Promise((resolve, reject) => {
      getRoleList(data)
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // 获取权限树
  getRoute({ commit }, data) {
    return new Promise((resolve, reject) => {
      getRoute(data)
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // 获取用户权限角色
  getRole({ commit }, data) {
    return new Promise((resolve, reject) => {
      getRole(data)
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // 添加权限
  addRole({ commit }, data) {
    return new Promise((resolve, reject) => {
      addRole(data)
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // 更新权限
  updateRole({ commit }, data) {
    return new Promise((resolve, reject) => {
      updateRole(data)
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // 删除权限
  deleteRole({ commit }, data) {
    return new Promise((resolve, reject) => {
      deleteRole(data)
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          reject(error);
        });
    });
  }
};

export default {
  namespaced: true, // 命名空间
  state,
  mutations,
  actions
};
